package com.admission.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.admission.model.PayLog;
import com.admission.service.PayLogManager;

@Namespace("/base")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "/base/index.action", type = "redirect") })
public class ExamineePayAction extends CrudActionSupport<PayLog>{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8043878208538953808L;
	HttpServletRequest request=ServletActionContext.getRequest();
	private PayLog payLog;
	private PayLogManager payLogManager;
	private String userIdentify;
	private String examineeNo;

	public PayLog getPayLog() {
		return payLog;
	}

	public void setPayLog(PayLog payLog) {
		this.payLog = payLog;
	}

	public PayLogManager getPayLogManager() {
		return payLogManager;
	}

	public void setPayLogManager(PayLogManager payLogManager) {
		this.payLogManager = payLogManager;
	}

	public void setUserIdentify(String userIdentify) {
		this.userIdentify = userIdentify;
	}

	public void setExamineeNo(String examineeNo) {
		this.examineeNo = examineeNo;
	}
	
	
	public String login() throws Exception {
		return "examinee-pay-login";
	}
	
//	public void getAuthPic()throws IOException {
//		HttpServletResponse response=ServletActionContext.getResponse();
//	      HttpSession session=request.getSession();
//		ServletOutputStream out = response.getOutputStream(); // 获取输出流
//		response.setContentType("image/gif");
//		response.setHeader("Pragma", "No-cache");
//		response.setHeader("Cache-Control", "no-cache");
//		response.setDateHeader("Expires", 0);
////		BufferedImage image = AuthCode.makePic(session);
//		ImageIO.write(image, "JPEG", out);
//		out.close();
//
//	}
	
	/**
	 * 登录
	 * @return
	 * @throws Exception
	 */
	public String checkLogin() throws Exception {
		payLog = payLogManager.getPayLogByIdNum(userIdentify);
		if (payLog != null) {
			if (payLog.getExamieeNo().trim().equals(examineeNo)) {
			      HttpSession session=request.getSession(false);
				session.setAttribute("USER_IDNUM", payLog.getIdnum());
				session.setAttribute("USER_EXAMNO", payLog.getExamieeNo());
				return this.list();
			} else {
				this.addActionMessage("考生号不对");
			}
		} 
//		else {
//			this.addActionMessage("身份证号不对");
//		}
		return "examinee-pay-login";
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	public String print() {
		payLog = payLogManager.getPayLogByIdNum(request.getSession()
				.getAttribute("USER_IDNUM").toString());
		return "print";
	}

	@Override
	public String list() throws Exception {
		if (payLog == null) {
			payLog = payLogManager.getPayLogByIdNum(request.getSession()
					.getAttribute("USER_IDNUM").toString());
		}
		if (payLog.getPayStatus().equals("1")) {
			payLog.setPayStatus("已缴费");
		} else {
			payLog.setPayStatus("未缴费");
		}
		return "personal-pay-index";
	}

	/**
	 * 判断状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkConsult() throws Exception {
		if (payLogManager.checkConsult().equals("1"))
			return this.checkLogin();
		else
			return "fail";
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PayLog getModel() {
		// TODO Auto-generated method stub
		return null;
	}

}
